﻿using System.Collections.Generic;
using OASys.Biz.BizAccess;
using OASys.Entity;
using OASys.Entity.EntitySearch;
using OASys.Enum;
using OASys.Web.Base;
using Project.Common;
using Project.Web.Base.Utility;

namespace OASys.Web.Areas.ModuleSys.Models
{
	/// <summary>
	/// 日志Index Model
	/// 作者：尤啸
	/// 时间：2012-06-27
	/// </summary>
	public class ModelLogsIndex : BaseModel
	{
        /// <summary>
        /// 日志业务逻辑
        /// 作者：尤啸
        /// 时间：2012-06-27
        /// </summary>
        private BizUVSysLogs bizUVSysLogs;

        /// <summary>
        /// 查询条件实体
        /// 作者：尤啸
        /// 时间：2012-06-27
        /// </summary>
        private SearchLogs searchEntity;
        /// <summary>
        /// 查询条件实体
        /// 作者：尤啸
        /// 时间：2012-06-27
        /// </summary>
        public SearchLogs SearchEntity
		{
			get
			{
				if (searchEntity == null)
                    return new SearchLogs();
                if (string.IsNullOrEmpty(searchEntity._OrderName))
                {
                    searchEntity._OrderName = string.Format("{0}",
                        ReflectionTools.GetPropertyNameFromExpression<UVSysLogs>(x => x.OperationTime));
                    searchEntity._OrderDirection = string.Format("{0}",
                        ((int)EnumOrder.DESC).ToString());
                }
				return searchEntity;
			}
			set { searchEntity = value; }
		}		

		/// <summary>
		/// 日志列表grid数据源
        /// 作者：尤啸
        /// 时间：2012-06-27
		/// </summary>
		public IList<UVSysLogs> GridDataSources
		{
			get;
			set;
		}
        /// <summary>
        /// 获取数据
        /// 作者：尤啸
        /// 时间：2012-06-27
        /// </summary>
		public void RetriveData()
		{
            //查询条件
            ExpressionCondition<UVSysLogs> condition = ExpressionCondition<UVSysLogs>.GetInstance();

            if (!string.IsNullOrEmpty(SearchEntity._CommonSearchCondition))
            {
                //模糊查询
                condition.Or(x => x.LogContent, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like).
                    Or(x => x.UserAccount, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like).
                    Or(x => x.OperatorName, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like);
                   
            }
            else
            {
                //条件查询
                condition.And(x => x.LogTypeID, SearchEntity._PLogTypeID, ExpressionValueRelation.Equal).
                                   And(x => x.OperatorName, SearchEntity._POperatorName, ExpressionValueRelation.Like).
                                   And(x => x.UserAccount, SearchEntity._PUserAccount, ExpressionValueRelation.Like).
                                   And(x => x.OperationTime, SearchEntity._POperationTimeFrom, ExpressionValueRelation.GreaterThanOrEqual).
                                   And(x => x.OperationTime, SearchEntity._POperationTimeTo, ExpressionValueRelation.LessThanOrEqual);
            }

            GridDataSources = bizUVSysLogs.PaginateListBy(SearchEntity._PageSize.Value,
                    SearchEntity._PageIndex ?? 0,
                    ref count,
                    condition.ConditionExpression,
                    SearchEntity._OrderName,
                    SearchEntity.EnumOrderDirection);
            //导出本页数据或者不导出逻辑            
            if (ExportObject.ExportType != null && ExportObject.ExportType == ExportType.导出本页)
            {
                ExportExcel(GridDataSources);
                return;
            } 
            
            //导出全部数据
            if (ExportObject.ExportType != null && ExportObject.ExportType == ExportType.导出全部)
            {
                GridDataSources = bizUVSysLogs.ListBy(condition.ConditionExpression, 
                    SearchEntity._OrderName,
                    SearchEntity.EnumOrderDirection);

                ExportExcel(GridDataSources);
                return;
            }

            //分页加载
			PaginateHelperObject = PaginateHelper.ConstructPaginate(SearchEntity._PageSize.Value, count, SearchEntity._PageIndex ?? 0, SearchEntity, "SearchEntity");			
		}

        /// <summary>
        /// 导出成Excel格式
        /// 作者：尤啸
        /// 时间：2012-06-27
        /// </summary>
        /// <param name="datasource">日志列表grid数据源</param>
        public void ExportExcel(IList<UVSysLogs> datasource)
        {
            if (ExportObject.ExportType == null)
                return;
            
            var exportInstance = ExportHelper.GetInstance();

            exportInstance.AddMapping<UVSysLogs, EnumLogType>(x => x.LogTypeID); //日志类型进行替换
            //exportInstance.AddMapping<UVSysLogs>(x => x.CompID, ReflectionTools.ComposeDictionaryFromCollection<UVAllOrgInfo>(AllOrgInfoCollection, x => x.ID, x => x.CompanyName)); //所属企业

            exportInstance.ExportExcel<UVSysLogs>(datasource, ExportObject, "日志");
        }
	}
}